package stdio.costa.moca.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

public class MyAuthorizationServlet extends HttpServlet {

	private static final long serialVersionUID = -865395705030455290L;

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		this.doPost(request, response);
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		String param = request.getParameter("param");

		System.out.println(param);

		DefaultHttpClient httpclient = new DefaultHttpClient();
		
		HttpGet httpGet = new HttpGet("http://localhost/moca/rest/authorization/getRequestTokenUrl");
		
		HttpResponse httpResponse = httpclient.execute(httpGet);

		HttpEntity entity = httpResponse.getEntity();

		System.out.println("Login form get: " + httpResponse.getStatusLine());
		
		String content = EntityUtils.toString(entity);
		
		System.out.println(content);
		
		response.sendRedirect(content);
		
	}
}
